<?php

namespace App\Services;

use App\Http\Requests\Mini\Meter\CollectorMeter;
use App\Http\Resources\Home\OrderResource\CreateOrderAfterCollection;
use App\Models\Address;
use App\Models\Cart;
use App\Models\CouponLog;
use App\Models\ElectricCollector;
use App\Models\ElectricCommunity;
use App\Models\ElectricElectricity;
use App\Models\Freight;
use App\Models\Goods;
use App\Models\GoodsSku;
use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\OrderPay;
use App\Models\Store;
use App\Traits\HelperTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 *采集器
 * Class ElectricMeterService
 * @package App\Services
 */
class CollectorMeterService extends BaseService
{

    use HelperTrait;

    /**
     * 2021/3/15--14:45
     * @effect:采集器列表
     * @return array
     * @author:苏冬冬
     * @email:1299273928@qq.com
     * @wechat:176158364793
     * @QQ:1299273928
     * @method:get_list
     */
    public function get_list()
    {
        $collector_model = new ElectricCollector();
        if (!empty(request()->tableAddress)) {
            $collector_model = $collector_model->where('tableAddress', 'like', '%' . request()->tableAddress . '%');
        }
        if (request()->code != '') {
            $collector_model = $collector_model->where('code', 'like', '%' . request()->code . '%');
        }
        if (request()->plan_id != '') {
            $collector_model = $collector_model->where('plan_id', 'like', '%' . request()->plan_id . '%');
        }
        if (request()->charge_id != '') {
            $collector_model = $collector_model->where('charge_id', 'like', '%' . request()->charge_id . '%');
        }
        if (request()->collector_id != '') {
            $collector_model = $collector_model->where('collector_id', 'like', '%' . request()->collector_id . '%');
        }
        // 下单时间
        $created_at = request()->created_at;
        if (!empty($created_at)) {
            $collector_model = $collector_model->whereBetween('created_at', [$created_at[0], $created_at[1]]);
        }
        $collector_model = $collector_model->orderBy('id', 'desc')->paginate($request->per_page ?? 30);

        return $this->format($collector_model);

    }

    /**
     * 2021/3/15--16:37
     * @effect:添加采集器操作
     * @param CollectorMeter $collectorMeter
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:add
     */
    public function add($request): ?array
    {
        $meter_model = new ElectricCollector();
        $meter_model->name = $request->name ?? '';//采集器名称
        $meter_model->code = $request->code ?? '';//采集器编码
        $meter_model->community_id = $request->community_id ?? '';
        $meter_model->area_id = $request->area_id ?? '';//区域id
        $meter_model->tower_id = $request->tower_id ?? '';//楼栋
        $meter_model->agent_id = $request->agent_id ?? '';//代理商id
        $meter_model->remarks = $request->remarks ?? '';
        $meter_model->address = $request->address ?? '';//地址

        try {
            $meter_model->save();
            return $this->format($meter_model->id, "添加采集器成功");
        } catch (\Exception $e) {
            return $this->format_error("数据模型操作错误：" . $e->getMessage());
        }
    }

    /**
     * 2021/3/15--16:37
     * @effect:添加采集器操作
     * @param $request
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:add
     */
    public function edit($request): ?array
    {
        $m_model = new ElectricCollector();
        $meter_model = $m_model->find($request->id);
        $meter_model->name = $request->name ?? '';//采集器名称
        $meter_model->code = $request->code ?? '';//采集器编码
        $meter_model->community_id = $request->community_id ?? '';
        $meter_model->area_id = $request->area_id ?? '';//区域id
        $meter_model->tower_id = $request->tower_id ?? '';//楼栋
        $meter_model->agent_id = $request->agent_id ?? '';//代理商id
        $meter_model->remarks = $request->remarks ?? '';
        $meter_model->address = $request->address ?? '';//地址

        try {
            $meter_model->save();
            return $this->format($meter_model->id, '编辑采集器成功');
        } catch (\Exception $e) {
            return $this->format_error('数据模型操作错误：' . $e->getMessage());
        }
    }
}
